package com.yuyou.fn.timer.task;

import com.alibaba.fastjson.JSONObject;
import com.github.wens.mybatisplus.examples.Example;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHander;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.Period;
import com.yuyou.fn.educational.service.IPeriodService;
import com.yuyou.fn.educational.service.ITeacherContinueClassStatService;
import com.yuyou.fn.educational.service.ITeacherRemunerationService;
import com.yuyou.fn.educational.vo.ClassRemunerationRankVo;
import com.yuyou.fn.educational.vo.ClassTimeRankVo;
import com.yuyou.fn.educational.vo.RankByContinueClassVo;
import com.yuyou.fn.educational.vo.StudentAmountRankVo;
import com.yuyou.fn.platform.entity.AreaTeam;
import com.yuyou.fn.platform.service.IAreaTeamService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 计算老师排行榜
 * Created by wens on 2017/5/12.
 */
@JobHander(value="TeacherRankTask")
@Component
public class TeacherRankTask extends IJobHandler {

    static final String KEY_PRE="rank:%s:%s:%s:%s";

    @Resource
    private ITeacherRemunerationService teacherRemunerationService;

    @Resource
    private IPeriodService periodService ;

    @Resource
    private IAreaTeamService areaTeamService ;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private ITeacherContinueClassStatService teacherContinueClassStatService;

    @Override
    public ReturnT<String> execute(String... params) throws Exception {
        String day = Utils.formatDate(TimeUtils.now(),"yyyy-MM-dd");

        Example<AreaTeam> example = AreaTeam.createExample();
        List<AreaTeam> areaTeamList = areaTeamService.findListByExample(example);


        for(AreaTeam areaTeam : areaTeamList ){
            Period currentPeriod = periodService.findCurrentPeriod(areaTeam.getAreaTeamId());
            if(currentPeriod == null){
                continue;
            }
            //课时
            List<ClassTimeRankVo> classTimeRankVoList = teacherRemunerationService.classTimeRank(areaTeam.getAreaTeamId() , currentPeriod.getPeriodId());
            redisTemplate.opsForValue().set(String.format(KEY_PRE ,areaTeam.getAreaTeamId() , currentPeriod.getPeriodId(),"classTime" , day ), JSONObject.toJSONString(classTimeRankVoList),2, TimeUnit.DAYS );
            //人次
            List<StudentAmountRankVo> studentAmountRankVoList = teacherRemunerationService.studentAmountRank(areaTeam.getAreaTeamId(), currentPeriod.getPeriodId());
            redisTemplate.opsForValue().set(String.format(KEY_PRE ,areaTeam.getAreaTeamId() , currentPeriod.getPeriodId(),"studentAmount" , day ), JSONObject.toJSONString(studentAmountRankVoList),2, TimeUnit.DAYS );
            //课酬
            List<ClassRemunerationRankVo> classRemunerationRankVoList = teacherRemunerationService.classRemunerationRank(areaTeam.getAreaTeamId(), currentPeriod.getPeriodId());
            redisTemplate.opsForValue().set(String.format(KEY_PRE ,areaTeam.getAreaTeamId() , currentPeriod.getPeriodId(),"classRemuneration" , day ), JSONObject.toJSONString(classRemunerationRankVoList),2, TimeUnit.DAYS );
            //续读

            List<RankByContinueClassVo> rankByContinueClassVoList = teacherContinueClassStatService.rank(areaTeam.getAreaTeamId(), currentPeriod.getPeriodId() );
            redisTemplate.opsForValue().set(String.format(KEY_PRE ,areaTeam.getAreaTeamId() , currentPeriod.getPeriodId(),"rankByContinueClass" , day ), JSONObject.toJSONString(rankByContinueClassVoList),2, TimeUnit.DAYS );

        }

        return ReturnT.SUCCESS ;


    }


}
